package com.ruoyi.tcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.DeletedEnum;
import com.ruoyi.common.utils.CardReaderStatusUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tcp.domain.CardReader;
import com.ruoyi.tcp.entity.beat.BeatEntity;
import com.ruoyi.tcp.enums.AccessStatusEnum;
import com.ruoyi.tcp.enums.DeviceStatusEnum;
import com.ruoyi.tcp.mapper.CardReaderMapper;
import com.ruoyi.tcp.service.ICardReaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 读卡器Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-29
 */
@Service
@Slf4j
public class CardReaderServiceImpl extends ServiceImpl<CardReaderMapper, CardReader> implements ICardReaderService {


    @Override
    public Page<CardReader> pageList(Page<CardReader> page, CardReader cardReader) {
        LambdaQueryWrapper<CardReader> queryWrapper = getQueryWrapper(cardReader);
        return page(page, queryWrapper);
    }

    @Override
    public List<CardReader> listAll(CardReader cardReader) {
        return list(getQueryWrapper(cardReader));
    }

    private LambdaQueryWrapper<CardReader> getQueryWrapper(CardReader cardReader) {
        LambdaQueryWrapper<CardReader> queryWrapper = Wrappers.lambdaQuery(CardReader.class);
        queryWrapper.eq(CardReader::getDeleted, DeletedEnum.NOMAL.getValue());

//        设备品牌
        if (StringUtils.isNotEmpty(cardReader.getDeviceBrand())) {
            queryWrapper.like(CardReader::getDeviceBrand, cardReader.getDeviceBrand());
        }


//        设备型号
        if (StringUtils.isNotEmpty(cardReader.getDeviceModel())) {
            queryWrapper.like(CardReader::getDeviceModel, cardReader.getDeviceModel());
        }


//        设备状态 1 在线 0 离线
        if (Objects.nonNull(cardReader.getDeviceStatus())) {
            queryWrapper.eq(CardReader::getDeviceStatus, cardReader.getDeviceStatus());
        }


//        设备名称
        if (StringUtils.isNotEmpty(cardReader.getDeviceName())) {
            queryWrapper.like(CardReader::getDeviceName, cardReader.getDeviceName());
        }


//        设备序列号
        if (StringUtils.isNotEmpty(cardReader.getSerialNumber())) {
            queryWrapper.like(CardReader::getSerialNumber, cardReader.getSerialNumber());
        }


//        省份名称
        if (StringUtils.isNotEmpty(cardReader.getProviceName())) {
            queryWrapper.like(CardReader::getProviceName, cardReader.getProviceName());
        }


//        省份编码
        if (StringUtils.isNotEmpty(cardReader.getProviceCode())) {
            queryWrapper.eq(CardReader::getProviceCode, cardReader.getProviceCode());
        }


//        市名称
        if (StringUtils.isNotEmpty(cardReader.getCityName())) {
            queryWrapper.like(CardReader::getCityName, cardReader.getCityName());
        }


//        市编号
        if (StringUtils.isNotEmpty(cardReader.getCityCode())) {
            queryWrapper.eq(CardReader::getCityCode, cardReader.getCityCode());
        }


//        区名称
        if (StringUtils.isNotEmpty(cardReader.getAreaName())) {
            queryWrapper.like(CardReader::getAreaName, cardReader.getAreaName());
        }


//        区编码
        if (StringUtils.isNotEmpty(cardReader.getAreaCode())) {
            queryWrapper.eq(CardReader::getAreaCode, cardReader.getAreaCode());
        }


//        地址
        if (StringUtils.isNotEmpty(cardReader.getAddress())) {
            queryWrapper.like(CardReader::getAddress, cardReader.getAddress());
        }


//        社区
        if (StringUtils.isNotEmpty(cardReader.getCommunity())) {
            queryWrapper.like(CardReader::getCommunity, cardReader.getCommunity());
        }


//        小区名称
        if (StringUtils.isNotEmpty(cardReader.getResidentialName())) {
            queryWrapper.like(CardReader::getResidentialName, cardReader.getResidentialName());
        }


//        ip
        if (StringUtils.isNotEmpty(cardReader.getIp())) {
            queryWrapper.eq(CardReader::getIp, cardReader.getIp());
        }


//        物理地址
        if (StringUtils.isNotEmpty(cardReader.getMacAddress())) {
            queryWrapper.eq(CardReader::getMacAddress, cardReader.getMacAddress());
        }


//        投放点
        if (StringUtils.isNotEmpty(cardReader.getPlacement())) {
            queryWrapper.like(CardReader::getPlacement, cardReader.getPlacement());
        }


//        登记时间
        if (Objects.nonNull(cardReader.getRegisterTime())) {
            queryWrapper.eq(CardReader::getRegisterTime, cardReader.getRegisterTime());
        }


//        接入状态
        if (Objects.nonNull(cardReader.getAccessStatus())) {
            queryWrapper.eq(CardReader::getAccessStatus, cardReader.getAccessStatus());
        }


//        接入时间
        if (Objects.nonNull(cardReader.getAccessTime())) {
            queryWrapper.eq(CardReader::getAccessTime, cardReader.getAccessTime());
        }


//        街道名称
        if (StringUtils.isNotEmpty(cardReader.getStreetName())) {
            queryWrapper.like(CardReader::getStreetName, cardReader.getStreetName());
        }


//        街道id
        if (StringUtils.isNotEmpty(cardReader.getStreetCode())) {
            queryWrapper.eq(CardReader::getStreetCode, cardReader.getStreetCode());
        }

        if(Objects.nonNull(cardReader.getPushType())){
            queryWrapper.eq(CardReader::getPushType, cardReader.getPushType());
        }

        return queryWrapper;
    }
    @Override
    public boolean offline(String deviceSerializeNumber) {
        if (StringUtils.isBlank(deviceSerializeNumber)) {
            return false;
        }
        Long status = CardReaderStatusUtil.getCardReaderDeviceStatus(deviceSerializeNumber);
        if(DeviceStatusEnum.OFFLINE.getCode().equals(status)){
            return true;
        }
        CardReader cardReader = getCardReaderBySerialNumber(deviceSerializeNumber);
        CardReader updateCardReader = new CardReader();
        updateCardReader.setId(cardReader.getId());
        updateCardReader.setDeviceStatus(DeviceStatusEnum.OFFLINE.getCode());
        updateCardReader.setUpdateTime(new Date());
        updateCardReader.setUpdateBy("系统");
        CardReaderStatusUtil.setCardReaderDeviceStatus(deviceSerializeNumber, DeviceStatusEnum.OFFLINE.getCode());
        return updateById(updateCardReader);
    }

    @Override
    public boolean saveOrUpdateByDeviceNumber(BeatEntity beatEntity, String clientIP) {
        if (StringUtils.isBlank(beatEntity.getDeviceSerializeNumber())) {
            return false;
        }
        Long status = CardReaderStatusUtil.getCardReaderDeviceStatus(beatEntity.getDeviceSerializeNumber());
        if(Objects.nonNull(status) && DeviceStatusEnum.ONLINE.getCode().equals(status)){
            //当前设备是在线状态
            return true;
        }


        CardReader cardReader = getCardReaderBySerialNumber(beatEntity.getDeviceSerializeNumber());
        if (Objects.isNull(cardReader)) {
            log.error("设备编号:{}未找到对应设备", beatEntity.getDeviceSerializeNumber());
            return false;
        }
        CardReader updateCardReader = new CardReader();;
        updateCardReader.setId(cardReader.getId());
        if(Objects.isNull(cardReader.getAccessTime())){
            updateCardReader.setAccessTime(LocalDateTime.now());
        }
        updateCardReader.setDeviceStatus(DeviceStatusEnum.ONLINE.getCode());
        updateCardReader.setIp(clientIP);
        updateCardReader.setAccessStatus(AccessStatusEnum.ONLINE.getCode());
        updateCardReader.setUpdateTime(new Date());
        updateCardReader.setUpdateBy("系统");
        updateById(updateCardReader);
        //设置缓存已在线
        CardReaderStatusUtil.setCardReaderDeviceStatus(beatEntity.getDeviceSerializeNumber(), DeviceStatusEnum.ONLINE.getCode());
        return true;
    }

    private  CardReader getCardReaderBySerialNumber(String deviceSerializeNumber) {
        LambdaQueryWrapper<CardReader> wrapper = Wrappers.lambdaQuery(CardReader.class)
                .eq(CardReader::getSerialNumber, deviceSerializeNumber)
                .eq(CardReader::getDeleted, DeletedEnum.NOMAL.getValue());
        wrapper.last("limit 1");

        return getOne(wrapper);
    }
}
